using System;
using System.ComponentModel;
using System.Data;
using System.Collections.Generic;
using System.Text;

namespace PlacementManager
{
    public partial class AddressComponent
    {
        /// <summary>
        /// Selects one record from the database table.
        /// Selects ths primary address for a venue.
        /// </summary>
        /// <returns>An object of a type representing a row in the database table.</returns>
        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public Address SelectPrimaryAddressForVenue(int venueId)
        {
            PlacementManager.DAL.Address_DAL o = new PlacementManager.DAL.Address_DAL();
           
            DataTable dt = o.SelectPrimaryAddressForVenue(venueId).Tables[0];

            if (dt.Rows.Count > 0)
            {
                DataRow row = dt.Rows[0];
                PlacementManager.Address AddressToReturn = new PlacementManager.Address((int)row["AddressId"], (string)row["Address1"], (string)row["Address2"], (string)row["Street"], (string)row["City"], (string)row["PostCode"], (int)row["StateId"], (int)row["CountryId"], (bool)row["IsPostal"], (double)row["Latitude"], (double)row["Longitude"], (bool)row["IsActive"], (DateTime)row["DateCreated"], (string)row["CreatedBy"], (DateTime)row["DateUpdated"], (string)row["UpdatedBy"]);
                return AddressToReturn;
            }
            else
            {
                PlacementManager.Address AddressToReturnEmpty = new PlacementManager.Address();
                return AddressToReturnEmpty;
            }
        }

        /// <summary>
        /// Selects one record from the database table.
        /// Selects ths primary address for a venue.
        /// </summary>
        /// <returns>An object of a type representing a row in the database table.</returns>
        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public Address SelectPrimaryAddressForContact(int contactId)
        {
            PlacementManager.DAL.Address_DAL o = new PlacementManager.DAL.Address_DAL();

            DataTable dt = o.SelectPrimaryAddressForContact(contactId).Tables[0];

            if (dt.Rows.Count > 0)
            {
                DataRow row = dt.Rows[0];
                PlacementManager.Address AddressToReturn = new PlacementManager.Address((int)row["AddressId"], (string)row["Address1"], (string)row["Address2"], (string)row["Street"], (string)row["City"], (string)row["PostCode"], (int)row["StateId"], (int)row["CountryId"], (bool)row["IsPostal"], (double)row["Latitude"], (double)row["Longitude"], (bool)row["IsActive"], (DateTime)row["DateCreated"], (string)row["CreatedBy"], (DateTime)row["DateUpdated"], (string)row["UpdatedBy"]);
                return AddressToReturn;
            }
            else
            {
                PlacementManager.Address AddressToReturnEmpty = new PlacementManager.Address();
                return AddressToReturnEmpty;
            }
        }

        /// <summary>
        /// Selects all records from the database table.
        /// </summary>
        /// <returns>A collection of objects of a type representing a row in the database table.</returns>
        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public List<Address> SelectAllForVenue(int venueId, string sortExpression)
        {
            List<Address> listAddress = new List<Address>();
            PlacementManager.DAL.Address_DAL o = new PlacementManager.DAL.Address_DAL();

            DataSet ds = o.SelectAllForVenue(venueId);

            foreach (DataRow row in ds.Tables[0].Rows)
            {
                listAddress.Add(new Address((int)row["AddressId"], (string)row["Address1"], (string)row["Address2"], (string)row["Street"], (string)row["City"], (string)row["PostCode"], (int)row["StateId"], (int)row["CountryId"], (bool)row["IsPostal"], (double)row["Latitude"], (double)row["Longitude"], (bool)row["IsActive"], (DateTime)row["DateCreated"], (string)row["CreatedBy"], (DateTime)row["DateUpdated"], (string)row["UpdatedBy"]));
            }

            listAddress.Sort(new AddressComparer(sortExpression));
            return listAddress;
        }

        /// <summary>
        /// Selects one record from the database table using the primary key.
        /// </summary>
        /// <returns>An object of a type representing a row in the database table.</returns>
        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public Address SelectOneWAddress1(string address1)
        {
            PlacementManager.DAL.Address_DAL o = new PlacementManager.DAL.Address_DAL();
            DataTable dt = o.SelectOneWAddress1(address1).Tables[0];

            if (dt.Rows.Count > 0)
            {
                DataRow row = dt.Rows[0];
                PlacementManager.Address AddressToReturn = new PlacementManager.Address((int)row["AddressId"], (string)row["Address1"], (string)row["Address2"], (string)row["Street"], (string)row["City"], (string)row["PostCode"], (int)row["StateId"], (int)row["CountryId"], (bool)row["IsPostal"], (double)row["Latitude"], (double)row["Longitude"], (bool)row["IsActive"], (DateTime)row["DateCreated"], (string)row["CreatedBy"], (DateTime)row["DateUpdated"], (string)row["UpdatedBy"]);
                return AddressToReturn;
            }
            else
            {
                PlacementManager.Address AddressToReturnEmpty = new PlacementManager.Address();
                return AddressToReturnEmpty;
            }
        }

        public string GetFormattedAddress(int addressId)
        {
            PlacementManager.AddressComponent oAddressC = new AddressComponent();
            PlacementManager.Address oAddress = oAddressC.SelectOne(addressId);

            string address1 = oAddress.Address1;
            string address2 = oAddress.Address2;
            string street = oAddress.Street;
            string city = oAddress.City;
            string postcode = oAddress.PostCode;
            bool isPostal = oAddress.IsPostal;
            string state = this.GetStateName(oAddress.StateId);
            string country = this.GetCountryName(oAddress.CountryId);

            StringBuilder sb = new StringBuilder();

            sb.Append(address1 + "<br />");
            if(address2 != null && address2.Length > 0)
            {
                sb.Append(address2 + "<br />");
            }
            if (street != null && street.Length > 0)
            {
                sb.Append(street + "<br />");
            }
            sb.Append(city + "<br />");
            sb.Append(postcode + "<br />");
            sb.Append(state + "<br />");
            sb.Append(country);

            return sb.ToString();
        }

        public string GetCityState(int addressId)
        {
            PlacementManager.AddressComponent oAddressC = new AddressComponent();
            PlacementManager.Address oAddress = oAddressC.SelectOne(addressId);

            string city = oAddress.City;
            string stateCode = this.GetStateCode(oAddress.StateId);

            return city + ", " + stateCode;
        }

        protected string GetStateName(int administrativeDivisionId)
        {
            PlacementManager.Geo_AdministrativeDivisionComponent oAdminDivC = new Geo_AdministrativeDivisionComponent();
            PlacementManager.Geo_AdministrativeDivision oAdminDiv = oAdminDivC.SelectOne(administrativeDivisionId);

            return oAdminDiv.DivisionName;
        }

        protected string GetStateCode(int administrativeDivisionId)
        {
            PlacementManager.Geo_AdministrativeDivisionComponent oAdminDivC = new Geo_AdministrativeDivisionComponent();
            PlacementManager.Geo_AdministrativeDivision oAdminDiv = oAdminDivC.SelectOne(administrativeDivisionId);

            return oAdminDiv.DivisionPostalAbbreviation;
        }

        protected string GetCountryName(int countryId)
        {
            PlacementManager.Geo_CountryComponent oCountryC = new Geo_CountryComponent();
            PlacementManager.Geo_Country oCountry = oCountryC.SelectOne(countryId);

            return oCountry.CountryName;
        }
    
        public void SetVenuePrimaryAddress(int addressId)
        {
            PlacementManager.DAL.Address_DAL o = new PlacementManager.DAL.Address_DAL();
            o.SetVenuePrimaryAddress(addressId);
        }

        public void SetContactPrimaryAddress(int addressId)
        {
            PlacementManager.DAL.Address_DAL o = new PlacementManager.DAL.Address_DAL();
            o.SetContactPrimaryAddress(addressId);
        }


        /// <summary>
        /// Selects one record from the database table.
        /// Selects ths primary address for a venue.
        /// </summary>
        /// <returns>An object of a type representing a row in the database table.</returns>
        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public Address SelectPrimaryAddressForUser(int userId)
        {
            PlacementManager.DAL.Address_DAL o = new PlacementManager.DAL.Address_DAL();

            DataTable dt = o.SelectPrimaryAddressForUser(userId).Tables[0];

            if (dt.Rows.Count > 0)
            {
                DataRow row = dt.Rows[0];
                PlacementManager.Address AddressToReturn = new PlacementManager.Address((int)row["AddressId"], (string)row["Address1"], (string)row["Address2"], (string)row["Street"], (string)row["City"], (string)row["PostCode"], (int)row["StateId"], (int)row["CountryId"], (bool)row["IsPostal"], (double)row["Latitude"], (double)row["Longitude"], (bool)row["IsActive"], (DateTime)row["DateCreated"], (string)row["CreatedBy"], (DateTime)row["DateUpdated"], (string)row["UpdatedBy"]);
                return AddressToReturn;
            }
            else
            {
                PlacementManager.Address AddressToReturnEmpty = new PlacementManager.Address();
                return AddressToReturnEmpty;
            }
        }

        /// <summary>
        /// Selects one record from the database table using the primary key.
        /// </summary>
        /// <returns>An object of a type representing a row in the database table.</returns>
        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public Address SelectOneWContactId(int contactId)
        {
            PlacementManager.DAL.Address_DAL o = new PlacementManager.DAL.Address_DAL();
            DataTable dt = o.SelectOneWContactId(contactId).Tables[0];

            if (dt.Rows.Count > 0)
            {
                DataRow row = dt.Rows[0];
                PlacementManager.Address AddressToReturn = new PlacementManager.Address((int)row["AddressId"], (string)row["Address1"], (string)row["Address2"], (string)row["Street"], (string)row["City"], (string)row["PostCode"], (int)row["StateId"], (int)row["CountryId"], (bool)row["IsPostal"], (double)row["Latitude"], (double)row["Longitude"], (bool)row["IsActive"], (DateTime)row["DateCreated"], (string)row["CreatedBy"], (DateTime)row["DateUpdated"], (string)row["UpdatedBy"]);
                return AddressToReturn;
            }
            else
            {
                PlacementManager.Address AddressToReturnEmpty = new PlacementManager.Address();
                return AddressToReturnEmpty;
            }
        }

        /// <summary>
        /// Selects all records from the database table.
        /// </summary>
        /// <returns>A collection of objects of a type representing a row in the database table.</returns>
        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public List<Address> SelectAllForContact(int contactId, string sortExpression)
        {
            List<Address> listAddress = new List<Address>();
            PlacementManager.DAL.Address_DAL o = new PlacementManager.DAL.Address_DAL();

            DataSet ds = o.SelectAllForContact(contactId);

            foreach (DataRow row in ds.Tables[0].Rows)
            {
                listAddress.Add(new Address((int)row["AddressId"], (string)row["Address1"], (string)row["Address2"], (string)row["Street"], (string)row["City"], (string)row["PostCode"], (int)row["StateId"], (int)row["CountryId"], (bool)row["IsPostal"], (double)row["Latitude"], (double)row["Longitude"], (bool)row["IsActive"], (DateTime)row["DateCreated"], (string)row["CreatedBy"], (DateTime)row["DateUpdated"], (string)row["UpdatedBy"]));
            }

            listAddress.Sort(new AddressComparer(sortExpression));
            return listAddress;
        }

        /// <summary>
        /// Selects all records from the database table.
        /// </summary>
        /// <returns>A collection of objects of a type representing a row in the database table.</returns>
        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public List<Address> SelectAllForUser(int userId, string sortExpression)
        {
            List<Address> listAddress = new List<Address>();
            PlacementManager.DAL.Address_DAL o = new PlacementManager.DAL.Address_DAL();

            DataSet ds = o.SelectAllForUser(userId);

            foreach (DataRow row in ds.Tables[0].Rows)
            {
                listAddress.Add(new Address((int)row["AddressId"], (string)row["Address1"], (string)row["Address2"], (string)row["Street"], (string)row["City"], (string)row["PostCode"], (int)row["StateId"], (int)row["CountryId"], (bool)row["IsPostal"], (double)row["Latitude"], (double)row["Longitude"], (bool)row["IsActive"], (DateTime)row["DateCreated"], (string)row["CreatedBy"], (DateTime)row["DateUpdated"], (string)row["UpdatedBy"]));
            }

            listAddress.Sort(new AddressComparer(sortExpression));
            return listAddress;
        }
    }
}
